import { ScrollView } from "cc";
import { UIOpacity } from "cc";
import { rect } from "cc";
import { UITransform } from "cc";
import { Component } from "cc";
import { _decorator,Node } from "cc";

const { ccclass, property } = _decorator;


/**
 * 超出隐藏 可简单的实现虚拟列表
 */
@ccclass('JScrollExceedHide')
export default class JScrollExceedHide extends Component{

    //列表
    scroll:ScrollView;

    @property(UITransform)
    view:UITransform;

    @property(Node)
    content:Node;

    //上次更新的数量
    lastUpdate:number = 0;

    onLoad(){
        this.scheduleOnce(() => {
            this.scroll = this.getComponent(ScrollView);
            this.scroll.node.on('scrolling', this.onScrolling, this);
            this.onScrolling();
        })
    }
    
    //滑动事件
    onScrolling(){
        this.onUpdate();
    }

    onUpdate(){
        let children = this.content.children;

        // //动态隐藏显示
        // //获取 view 最高高度 和 最低高度
        // let viewWorld = this.view.node.worldPosition;
        // let viewSize = this.view.contentSize;
        // let anchorPoint = this.view.anchorPoint;

        // let top = viewWorld.y + ((viewSize.height/2) * (2 - (anchorPoint.y * 2)));
        // let bottom = viewWorld.y - ((viewSize.height/2) * (anchorPoint.y * 2));

        // let right = viewWorld.x + (viewSize.width/2 * (2 - (anchorPoint.x * 2)));
        // let left = viewWorld.x - (viewSize.width/2 * ((anchorPoint.x * 2)));

        
        let box = this.view.getBoundingBoxToWorld();

        children.forEach(child => {


            let childRect = child.getComponent(UITransform).getBoundingBoxToWorld();


            let opacity:UIOpacity;
            if(!(opacity = child.getComponent(UIOpacity))){
                opacity = child.addComponent(UIOpacity);
            }

            if(box.intersects(childRect)){
                opacity.opacity = 255;
            }else{
                opacity.opacity = 0;
            }

            // let ctop = childRect.center.y + ((childRect.height/2));
            // let cbottom = childRect.center.y - ((childRect.height/2));

            // let cright = childRect.center.x + ((childRect.width/2));
            // let cleft = childRect.center.x - ((childRect.width/2));

            // let opacity:UIOpacity;
            // if(!(opacity = child.getComponent(UIOpacity))){
            //     opacity = child.addComponent(UIOpacity);
            // }

            // //如果在框里面则显示 否则隐藏
            // if(
            //     ((top >= ctop && bottom <= ctop) || (top >= cbottom && bottom <= cbottom)) //Y 在内容中间
            //     &&
            //     ((right >= cright && left <= cright) || (right >= cleft && left <= cleft)) //X 在内容中间
            // ){
            // }else{
            //     opacity.opacity = 0;
            // }

        })

    }
    

}